package appinfo.controller;

import appinfo.pojo.DevUser;
import appinfo.service.DevUserService;
import appinfo.util.EmptyUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author lzfei
 */
@RequestMapping("/dev")
@Controller
public class DevUserController {
    @Resource
    private DevUserService devUserService;

    @RequestMapping("/main")
    public String main(){
        return "developer/main";
    }

    @RequestMapping("/login")
    public String toLogin(){
        return "devlogin";
    }

    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("devUserSession");
        return "devlogin";
    }


    @RequestMapping("/dologin")
    public String dologin(@RequestParam String devCode , @RequestParam String devPassword, HttpServletRequest request){
       if(EmptyUtils.isEmpty(devCode)|| EmptyUtils.isEmpty(devPassword)){
           request.setAttribute("error","用户名或密码不能为空！");
       }


        DevUser devUser =  devUserService.dologin(devCode,devPassword);
       if(EmptyUtils.isNotEmpty(devUser)){
           HttpSession session = request.getSession();
           session.setAttribute("devUserSession",devUser);
           return "redirect:/dev/main";
       }else {
           request.setAttribute("error","用户名或密码错误");
           return "devlogin";
       }

    }
}


